package com.hb.trade.server.flow.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hb.core.pojo.flow.mapper.FlowTempMainMapper;
import com.hb.core.pojo.flow.entity.FlowTempApprover;
import com.hb.core.pojo.flow.entity.FlowTempFormField;
import com.hb.core.pojo.flow.entity.FlowTempMain;
import com.hb.core.pojo.flow.dto.FlowTempFormFieldDTO;
import com.hb.core.pojo.flow.dto.FlowTempMainDTO;
import com.hb.core.pojo.flow.dto.FlowTempNodeDTO;
import com.hb.core.pojo.flow.dto.FlowTempTaskDTO;
import com.hb.trade.server.flow.manager.FlowTempMainManager;
import com.hb.trade.server.flow.service.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 流程主表(模板) 服务实现类
 * </p>
 *
 * @author zhaojk
 * @since 2021-05-27
 */
@Service
public class FlowTempMainServiceImpl extends ServiceImpl<FlowTempMainMapper, FlowTempMain> implements FlowTempMainService {

    protected final FlowTempNodeService flowTempNodeService;
    protected final FlowTempTaskService flowTempTaskService;
    protected final FlowTempFormFieldService flowTempFormFieldService;
    protected final FlowTempApproverService flowTempApproverService;

    public FlowTempMainServiceImpl(FlowTempNodeService flowTempNodeService,
                                   FlowTempTaskService flowTempTaskService,
                                   FlowTempFormFieldService flowTempFormFieldService,
                                   FlowTempApproverService flowTempApproverService) {
        this.flowTempNodeService = flowTempNodeService;
        this.flowTempTaskService = flowTempTaskService;
        this.flowTempFormFieldService = flowTempFormFieldService;
        this.flowTempApproverService = flowTempApproverService;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String defineFlowTemp(FlowTempMainDTO flowTempMain) {
        Long tempMainId = flowTempMain.getId();
        // 不存在ID做新增操作
        if (tempMainId == null) {
            return saveFlowTemp(flowTempMain);
        }
        // 存在ID做更新操作
        return updateFlowTemp(flowTempMain);
    }

    @Override
    public boolean existsByFlowTempName(String flowName) {
        return baseMapper.countByName(flowName) > 0;
    }

    @Override
    public boolean existsByFlowTempId(Long id) {
        return baseMapper.countById(id) > 0;
    }

    @Override
    public FlowTempMainDTO getAllById(Long tempMainId) {
        // 查询流程模板主信息
        FlowTempMainDTO tempMainDto = baseMapper.getFlowTempMainChain(tempMainId);
        FlowTempNodeDTO nodeDto = flowTempNodeService.getTempNode(tempMainId);
        tempMainDto.setTempNode(nodeDto);
        return tempMainDto;
    }

    private String saveFlowTemp(FlowTempMainDTO flowTempMain) {
        // 保存流程主表信息
        FlowTempMainManager tempMainManager = flowTempMain.to(FlowTempMainManager.class);
        tempMainManager.buildBaseData(1);

        // 设置流程节点总数
        List<FlowTempNodeDTO> nodes = tempMainManager.getNodeList(tempMainManager.getId());
        tempMainManager.setRunTotal(nodes.size());
        save(tempMainManager);

        // 声明流程模板ID
        Long tempId = tempMainManager.getId();
        if (tempId == null) {
            return "流程模板保存失败";
        }

        // 保存流程节点信息
        Long parentId = 0L;
        int index = 1;
        for (FlowTempNodeDTO node : nodes) {
            node.buildBaseData(1);
            node.setTempMainId(tempId);
            node.setParentId(parentId);
            node.setCurrentIndex(index);
            flowTempNodeService.save(node);

            // 保存流转任务信息
            List<FlowTempTaskDTO> tasks = node.getTempTasks();
            for (FlowTempTaskDTO taskDto : tasks) {
                taskDto.buildBaseData(1);
                taskDto.setTempNodeId(node.getId());
                flowTempTaskService.save(taskDto);

                // 保存审批人信息
                List<FlowTempApprover> approvers = taskDto.getApprovers();
                for (FlowTempApprover approver : approvers) {
                    approver.buildBaseData(1);
                    approver.setTempTaskId(taskDto.getId());
                    flowTempApproverService.save(approver);
                }
            }
            parentId = node.getId();
            index++;
        }
        // 保存表单信息
        List<FlowTempFormFieldDTO> fields = tempMainManager.getFormFieldWithTempMainId(tempId);
        if (CollUtil.isNotEmpty(fields)) {
            Collection<FlowTempFormField> fieldCollection =
                    CollUtil.trans(fields, (fieldDto) -> fieldDto.to(FlowTempFormField.class));
            flowTempFormFieldService.saveBatch(fieldCollection);
        }
        return null;
    }

    private String updateFlowTemp(FlowTempMainDTO flowTempMain) {
        return "流程模板暂不支持修改, 请删除之后重新定义";
    }

}
